package com.bestom.producetest.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.bestom.producetest.R;

import android.content.Intent;
import android.net.Uri;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bestom.producetest.base.BaseNfcActivity;
import com.bestom.producetest.helper.CommandExec;
import com.bestom.producetest.utils.ProperTiesUtils;

import java.io.IOException;

import static com.bestom.producetest.base.App.configfilename;

/**
 * Test NFC
 */
public class NFCActivity extends BaseNfcActivity implements View.OnClickListener {
    private static final String TAG = MainActivity.class.getSimpleName();

    static String RED_LIGHT_OPEN = "echo 200 > /sys/class/leds/red/brightness";
    static String RED_LIGHT_CLOSE = "echo 0 > /sys/class/leds/red/brightness";
    static String RED_LIGHT_BLN_OPEN = "echo 1 > /sys/class/leds/red/blink";
    static String RED_LIGHT_BLN_CLOSE = "echo 0 > /sys/class/leds/red/blink";

    static String BLUE_LIGHT_OPEN = "echo 200 > /sys/class/leds/blue/brightness";
    static String BLUE_LIGHT_CLOSE = "echo 0 > /sys/class/leds/blue/brightness";
    static String BLUE_LIGHT_BLN_OPEN = "echo 1 > /sys/class/leds/blue/blink";
    static String BLUE_LIGHT_BLN_CLOSE = "echo 0 > /sys/class/leds/blue/blink";

    static String GREEN_LIGHT_OPEN = "echo 200 > /sys/class/leds/green/brightness";
    static String GREEN_LIGHT_CLOSE = "echo 0 > /sys/class/leds/green/brightness";
    static String GREEN_LIGHT_BLN_OPEN = "echo 1 > /sys/class/leds/green/blink";
    static String GREEN_LIGHT_BLN_CLOSE = "echo 0 > /sys/class/leds/green/blink";

    LinearLayout red_layout, blue_layout, green_layout;
    private TextView mNfcText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc);
        Activity mActivity = this;
        final Context mContext = this;

        findViewById(R.id.close_btn).setOnClickListener(this);

        // for hide navigation bar
        findViewById(R.id.hide_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                senBroadcast("android.intent.action.HIDE_NAVIGATION_BAR");
            }
        });
        // for show navigation bar
        findViewById(R.id.show_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                senBroadcast("android.intent.action.SHOW_NAVIGATION_BAR");
            }
        });

        red_layout = findViewById(R.id.red_layout);
        findViewById(R.id.red_open).setOnClickListener(this);
        findViewById(R.id.red_close).setOnClickListener(this);
        findViewById(R.id.red_bln_open).setOnClickListener(this);
        findViewById(R.id.red_bln_close).setOnClickListener(this);

        blue_layout = findViewById(R.id.blue_layout);
        findViewById(R.id.blue_open).setOnClickListener(this);
        findViewById(R.id.blue_close).setOnClickListener(this);
        findViewById(R.id.blue_bln_open).setOnClickListener(this);
        findViewById(R.id.blue_bln_close).setOnClickListener(this);

        green_layout = findViewById(R.id.green_layout);
        findViewById(R.id.green_open).setOnClickListener(this);
        findViewById(R.id.green_close).setOnClickListener(this);
        findViewById(R.id.green_bln_open).setOnClickListener(this);
        findViewById(R.id.green_bln_close).setOnClickListener(this);

        mNfcText = findViewById(R.id.tv_nfc_text);

        red_layout.setVisibility(Integer.valueOf(ProperTiesUtils.getProperties(mActivity, configfilename, "NfcLightTest")) == 1 ? View.VISIBLE : View.GONE);
        blue_layout.setVisibility(Integer.valueOf(ProperTiesUtils.getProperties(mActivity, configfilename, "NfcLightTest")) == 1 ? View.VISIBLE : View.GONE);
        green_layout.setVisibility(Integer.valueOf(ProperTiesUtils.getProperties(mActivity, configfilename, "NfcLightTest")) == 1 ? View.VISIBLE : View.GONE);

        //get Tag object
        Tag detectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (detectedTag != null) {
            // read
            mNfcText.setText(readTag(detectedTag));
            sendOder(GREEN_LIGHT_BLN_OPEN);
        }
    }

    private void senBroadcast(String action) {
        Intent intent = new Intent(action);
        sendBroadcast(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                Intent intent = new Intent();
                intent.putExtra("request", "nfc");
                setResult(001, intent);
                finish();
                break;
            // Red light
            case R.id.red_open:
                sendOder(RED_LIGHT_OPEN);
                break;
            case R.id.red_close:
                sendOder(RED_LIGHT_CLOSE);
                break;
            case R.id.red_bln_open:
                sendOder(RED_LIGHT_BLN_OPEN);
                break;
            case R.id.red_bln_close:
                sendOder(RED_LIGHT_BLN_CLOSE);
                break;
            // Blue light
            case R.id.blue_open:
                sendOder(BLUE_LIGHT_OPEN);
                break;
            case R.id.blue_close:
                sendOder(BLUE_LIGHT_CLOSE);
                break;
            case R.id.blue_bln_open:
                sendOder(BLUE_LIGHT_BLN_OPEN);
                break;
            case R.id.blue_bln_close:
                sendOder(BLUE_LIGHT_BLN_CLOSE);
                break;
            // Green light
            case R.id.green_open:
                sendOder(GREEN_LIGHT_OPEN);
                break;
            case R.id.green_close:
                sendOder(GREEN_LIGHT_CLOSE);
                break;
            case R.id.green_bln_open:
                sendOder(GREEN_LIGHT_BLN_OPEN);
                break;
            case R.id.green_bln_close:
                sendOder(GREEN_LIGHT_BLN_CLOSE);
                break;
            default:
        }
    }

    /**
     * run exec
     */
    private void sendOder(String order) {
        CommandExec.execCommand(order, false);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        //1.get Tag object
        Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        //read
        mNfcText.setText(readTag(detectedTag));
        sendOder(GREEN_LIGHT_BLN_OPEN);
    }

    /**
     * write NFC TAG
     */
    private void writeNfcTag(Tag tag) {

        if (tag == null) return;
        NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{NdefRecord.createUri(Uri.parse("https://www.baidu.com/"))});
        int size = ndefMessage.toByteArray().length;
        Ndef ndef = Ndef.get(tag);
        try {
            if (ndef != null) {
                ndef.connect();
                if (!ndef.isWritable()) return;
                if (ndef.getMaxSize() < size) return;

                ndef.writeNdefMessage(ndefMessage);
                Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
            } else {
                NdefFormatable ndefFormatable = NdefFormatable.get(tag);
                if (ndefFormatable != null) {
                    ndefFormatable.connect();
                    ndefFormatable.format(ndefMessage);
                    Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "format is failed", Toast.LENGTH_SHORT).show();
                }
            }
        } catch (IOException | FormatException e) {
            e.printStackTrace();
        }
    }

    /**
     * read NFC TAG
     */
    public String readTag(Tag tag) {
        MifareClassic mfc = MifareClassic.get(tag);
        for (String tech : tag.getTechList()) {
            System.out.println(tech);
        }
        try {
            String metaInfo = "";
            //Enable I/O operations to the tag from this TagTechnology object.
            mfc.connect();
            //get TAG type
            int type = mfc.getType();
            //get TAG The number of sectors contained
            int sectorCount = mfc.getSectorCount();
            String typeS = "";
            switch (type) {
                case MifareClassic.TYPE_CLASSIC:
                    typeS = "TYPE_CLASSIC";
                    break;
                case MifareClassic.TYPE_PLUS:
                    typeS = "TYPE_PLUS";
                    break;
                case MifareClassic.TYPE_PRO:
                    typeS = "TYPE_PRO";
                    break;
                case MifareClassic.TYPE_UNKNOWN:
                    typeS = "TYPE_UNKNOWN";
                    break;
                default:
                    break;
            }
            metaInfo += "Card Type：" + typeS + "\nSector:：" + sectorCount + "\nBlock: "
                    + mfc.getBlockCount() + "\nStorage Space: " + mfc.getSize() + "B\n";
            return metaInfo;
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } finally {
            if (mfc != null) {
                try {
                    mfc.close();
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG)
                            .show();
                }
            }
        }
        return null;
    }
}

